
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

namespace Reservations
{
	public class RoomAllocationBizRules : Syrinx.BaseBizRules
	{
		public IBizObjList getRoomAllocationOverrides(IApplication app, IRuleContext context, Swaf.BGuid.Guid companyId)
		{
			context.put("companyId", companyId==null?ActiveCompanyId:companyId);
			IBizObjList allocations = app.appData.get("DA.Reservations.GetBookingAgentAllocationOverrides", context) as IBizObjList;
			return allocations;
		}

		public void updateRoomAllocationOverride(IApplication app, IRuleContext context, IBizObj allocation)
		{
			if (allocation.get("fromDate") == null)
				allocation.put("fromDate", DateTime.Now.Date);
			if (allocation.get("toDate") == null)
				allocation.put("toDate", DateTime.Now.Date);

			app.appData.put("DA.Reservations.DeleteRoomAllocationOverride", allocation);
			app.appData.put("DA.Reservations.AddRoomAllocationOverride", allocation);
		}

		public void deleteRoomAllocationOverride(IApplication app, IRuleContext context, Swaf.BGuid.Guid allocationId)
		{
			context.put("id", allocationId);
			app.appData.put("DA.Reservations.DeleteRoomAllocationOverride", context);
		}

		public void updateDefaultRoomAllocation(IApplication app, IRuleContext context, IBizObj bookingAgent)
		{
			IBizObjList allocations = bookingAgent.get("roomTypeAllocations") as IBizObjList;
			context.put("agent", bookingAgent);
			foreach (IBizObj alloc in allocations)
			{
				if (alloc.isDirty())
				{
					context.put("id", new Swaf.BGuid.Guid());
					context.put("alloc", alloc);
					app.appData.put("DA.Reservations.DeleteDefaultRoomAllocation", context);
					if (!alloc.get("quantity").Equals(0))
						app.appData.put("DA.Reservations.AddDefaultRoomAllocation", context);
				}
			}
		}

		public IBizObjList getBookAgents(IApplication app, IRuleContext context)
		{
			context.put("companyId", ActiveCompanyId);
			IBizObjList bookingAgents = app.appData.get("DA.Reservations.GetBookingAgents", context) as IBizObjList;
			foreach (IBizObj agent in bookingAgents)
				if (((IComparable)agent.get("id")).CompareTo(ActiveCompany.get("anonymousId")) == 0)
				{
					bookingAgents.Remove(agent);
					bookingAgents.Insert(0, agent);
					agent.put("contact.lastName", "All general internet users");
					break;
				}
			return bookingAgents;
		}

		public IBizObjList getBookAgentsWithDefaultRoomAllocations(IApplication app, IRuleContext context, Swaf.BGuid.Guid companyId)
		{
			IResults rc = context.RuleGroup.execute(context, "getBookAgents");
			if (rc.ExceptionInfo != null)
				throw (rc.ExceptionInfo);
			IBizObjList bookingAgents = rc.CallResults as IBizObjList;
			context.put("companyId", companyId==null?ActiveCompanyId:companyId);
			//Merge the default allocations into the list of booking agents.
			context.put("agents", bookingAgents);
			app.appData.get("DA.Reservations.GetBookingAgentDefaultAllocations", context);
			return bookingAgents;
		}

		public IBizObjList getRoomTypes(IApplication app, IRuleContext context)
		{
			context.put("companyId", ActiveCompanyId);
			IBizObjList roomTypes = app.appData.get("DA.Reservations.GetRoomTypeProducts", context) as IBizObjList;
			return roomTypes;
		}
	}
}
